var mongoose = require("mongoose");
var db = mongoose.connection;
var bannerSchema = new mongoose.Schema({
  //标题
  title:{type:String},
  //图片
  img:{type:String , required:true},
  //链接
  href:{type:String},
  //发布时间
  releaseTime:{type:String},
  //排序
  sort:{type:Number , default:0}
});
var bannerS = db.model("banner" , bannerSchema);
//方法
function Banner(){};
//增加
Banner.prototype.addBanner = function(data , callback){
  bannerS.create(data , function(error , data){
    if(error){
      return callback("添加失败");
    }else{
      return callback(null , "添加成功");
    };
  });
};
//获取列表
Banner.prototype.getBanner = function(data , callback){
  bannerS.find(data , null , {sort:{sort:-1}} , function(error , data){
    if(error){
      callback("获取失败")
    }else{
      callback(null , data);
    };
  });
};
//编辑
Banner.prototype.update = function(id , data , callback){
  bannerS.update({_id:id} , data , function(error , data){
    if(error){
      callback("更新失败");
    }else{
      callback(null , data);
    };
  });
};
module.exports = new Banner();
